import pandas as pd;
from GrpLabel_Item import GrpLabel_Item;

class GrpLabels_Mngr(object):
    def __init__(self, parent):
        self.dataModel = parent.dataModel;
        self.dTrackPaths = parent.dTrackPaths;
        self.gScene = parent.graphicsScene; #GraphicsScene of Movie
        self.gSceneView = parent.gSceneView; #SceneView of Graph Nodes
        self.dfGrpLbls = None;
        self.bShowGrpActs = True;
        self.nFrm = 0;
        
    def clear_GrpLabels(self):
        if self.dfGrpLbls is None: return; 
        for i in self.dfGrpLbls.index:
            grpLbl_Item = self.dfGrpLbls['GrpLbl_Item'][i];
            self.gScene.removeItem(grpLbl_Item);
            del grpLbl_Item;
        del self.dfGrpLbls;
        self.dfGrpLbls = None;
        
    def create_GrpLabelsTbl(self, dfGrpActs):
        aPhysActs = ["WALK", "STAND", "RUN"];
        dfPhysActs = dfGrpActs[dfGrpActs["Activity"].isin(aPhysActs)];
        dfGrpLbls = pd.DataFrame(columns=['Act-Ids', 'Start-Frm', 'End-Frm', 'Persons']);
        for actId in dfPhysActs.index:
            nStart = dfPhysActs['Start-Frm'][actId];
            nEnd = dfPhysActs['End-Frm'][actId];
            aPersons = dfPhysActs['Persons'][actId];
            aRow = [nStart, nEnd, aPersons];
            aActIds = [actId];
            nActId, nParentId = actId, dfPhysActs['Parent-Id'][actId];
            while dfGrpActs['Persons'][nActId]==dfGrpActs['Persons'][nParentId]:
                aActIds.append(nParentId);
                nActId = nParentId;
                nParentId = dfGrpActs['Parent-Id'][nActId];
            aActIds.reverse();
            aRow.insert(0, aActIds);
            dfGrpLbls = dfGrpLbls.append(pd.Series(aRow, dfGrpLbls.columns), ignore_index=True);
        self.dfGrpLbls = self.set_GrpLbls_Col(dfGrpLbls, dfGrpActs);
        self.create_GrpLbl_Items();
    
    def create_GrpLbl_Items(self):
        aGrpLbl_Items = [];
        for i in self.dfGrpLbls.index:
            aGrpLbl = self.dfGrpLbls["GrpLbls"][i];
            aTrackPaths_Grp = [];
            for nTrkId in self.dfGrpLbls["Persons"][i]:
                aTrackPaths_Grp.append(self.dTrackPaths[nTrkId]);
            grpLbl_Item = GrpLabel_Item(aGrpLbl, aTrackPaths_Grp, self.gSceneView);
            aGrpLbl_Items.append(grpLbl_Item);
            self.gScene.addItem(grpLbl_Item);
        self.dfGrpLbls.insert(2, "GrpLbl_Item", aGrpLbl_Items);

    def set_GrpLbls_Col(self, dfGrpLbls, dfGrpActs):
        aGrpLbls = [];
        for aActId in dfGrpLbls["Act-Ids"]:
            aLbls = [];
            for actId in aActId:
                sLbl = str(actId)+". ";
                sLbl += dfGrpActs["Activity"][actId]+" <";
                sLbl += dfGrpActs["Role"][actId]+">";
                aLbls.append(sLbl);
            aGrpLbls.append(aLbls);
        dfGrpLbls.insert(1, "GrpLbls", aGrpLbls);
        return dfGrpLbls;
        
    def updateGrpLabels(self, nFrm):
        if self.dfGrpLbls is None: return;
        if self.bShowGrpActs==False: return;
        self.nFrm = nFrm;
        for i in self.dfGrpLbls.index:
            if ((nFrm>self.dfGrpLbls['Start-Frm'][i] or 
                 (self.dfGrpLbls['Start-Frm'][i]==0 and nFrm==0)) and 
                (nFrm<=self.dfGrpLbls['End-Frm'][i])):
                grpLbl_Item = self.dfGrpLbls['GrpLbl_Item'][i];
                grpLbl_Item.setVisible(True);
                grpLbl_Item.updateSize(nFrm);
            else:
                self.dfGrpLbls['GrpLbl_Item'][i].setVisible(False);
                
    def setVisible(self, bVisible):
        self.bShowGrpActs = bVisible;
        if bVisible==False:
            for i in self.dfGrpLbls.index:
                self.dfGrpLbls['GrpLbl_Item'][i].setVisible(bVisible);
        self.updateGrpLabels(self.nFrm);
        